package com.everflourish.yeah100.act.test;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2016/11/30.
 */

public class NodeHellp {

    /**
     * 转换为node数据集
     *
     * @param mlist
     * @param <T>
     * @return
     */
    public static <T> List<Node> changedToNode(List<T> mlist) throws IllegalAccessException {
        List<Node> result = new ArrayList<>();
        Node node = null;
        for (T t : mlist) {
            Field field[] = t.getClass().getDeclaredFields();
            node = new Node();
            for (Field f : field) {
                if (f.getAnnotation(SubId.class) != null) {
                    f.setAccessible(true);
                    node.setId(f.get(t).toString());
                }
                if (f.getAnnotation(PerentId.class) != null) {
                    f.setAccessible(true);
                    node.setpId(f.get(t).toString());
                }

                if (f.getAnnotation(Title.class) != null){
                    f.setAccessible(true);
                    node.setName(f.get(t).toString());
                }
            }
            result.add(node);
        }

        //设置节点关系
        for (int i = 0; i < result.size(); i++) {
            Node ni = result.get(i);
            for (int j = 0; j < result.size(); j++) {
                Node nj = result.get(j);
                if (ni.getpId() == nj.getId()) {
                    nj.getChildren().add(ni);
                    ni.setParent(nj);
                } else if (nj.getpId() == ni.getId()) {
                    ni.getChildren().add(nj);
                    nj.setParent(ni);
                }
            }
        }

        return result;
    }


    /**
     * 过滤要显示的数据
     * @param mlist
     * @return
     */
    public static List<Node> filterVisibitNodes(List<Node> mlist){
        List<Node> result = new ArrayList<>();
        for (Node t : mlist){
            if (t.isRoot() || t.isParentExpend()){
                result.add(t);
            }
        }
        return result;
    }


    public static  void getRootNodes(){

    }
}
